/*
 * @Author: your name
 * @Description: 
 * @FilePath: /code/time/timer.h
 */
#pragma once
#include <iostream>
#include <functional>

class Timer
{
public:
    using TimeEventCallback = std::function<void()>;
    Timer(int32_t repeatedtimes,int64_t interval,TimeEventCallback& callback)
        :repeated_times_(repeatedtimes)
        ,interval_(interval)
        ,callback_(callback)
        {
            expired_time_ = time(nullptr) + interval_;
            id_ = generateId();
        }
    ~Timer() = default;
    bool IsExpired() const
    {
        return time(nullptr) >= expired_time_;
    }
    time_t ExpiredTime()
    {
        return expired_time_;
    }
    int64_t Id()
    {
        return id_;
    }
    int32_t RepeatTimes() {return repeated_times_;}
    void SetExpiredTime(int64_t t) {expired_time_ = t;}
    void Run();
    static int64_t generateId();
    
private:
    int64_t id_;
    time_t expired_time_;
    int32_t repeated_times_;
    int64_t interval_;

    TimeEventCallback callback_;

    static int64_t initial_id_;
    static std::mutex mutex_;
};